/**   
 * All rights Reserved, Designed By MiGu  
 * Copyright:    Copyright(C) 2016-2020  
 * Company       MiGu  Co., Ltd.
*/
package com.migu.spms.webapi;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.migu.spms.service.data.User;
import com.migu.spms.service.inf.IFileAttachService;
import com.migu.spms.webapi.data.FileAttachRequest;
import com.migu.spms.webapi.data.RequireAttachmentRequest;


/**   
 * FileUploadController类
 * 项目名称:  SPMS功能需求
 * 包名称:    com.migu.spms.webapi   
 * 类名称:    FileUploadController.java
 * 类描述:    文件上传
 * 创建人:    wcr    
 * 创建时间:  2017年6月19日 下午3:47:51   
 */
@Controller
@RequestMapping(value = "/requireFile", produces = "application/json;charset=UTF-8")
public class FileUploadController extends BaseController {

    private static Logger logger = LoggerFactory.getLogger(FileUploadController.class);

    @Resource(name = "fileAttachServiceImpl")
    private IFileAttachService fileAttachService;
    
    @ResponseBody  
    @RequestMapping(value = "/uploadAttach")
    public String uploadAttach(@RequestParam(value="uploadFile") MultipartFile file, HttpServletRequest req, 
            HttpServletResponse resp,String sessionId) throws Exception{
        User user = getUserBySession(req, sessionId);
        if (user == null || null == file) {
            return generateResponse(401);
        }
        return generateResponse(200);
    }
    
    @ResponseBody  
    @RequestMapping(value = "/addAttachMment")
    public String addAttachMment(HttpServletRequest req, 
            HttpServletResponse resp,String sessionId) throws Exception{
        User user = getUserBySession(req, sessionId);
        if (user == null ) {
            return generateResponse(401);
        }
        RequireAttachmentRequest msg = ParseMsg(req, RequireAttachmentRequest.class);
        fileAttachService.addAttachMment(msg);
        return generateResponse(200);
    }
    
    @ResponseBody  
    @RequestMapping(value = "/deleteAttach")
    public String deleteAttach(HttpServletRequest req, HttpServletResponse resp,String sessionId) throws Exception{
        User user = getUserBySession(req, sessionId);
        if (user == null) {
            return generateResponse(401);
        }
        FileAttachRequest msg = ParseMsg(req, FileAttachRequest.class);
        fileAttachService.deleteFileAttach(msg, sessionId);
        return generateResponse(200);
    }
}
